package com.fjzx.androidframe.global.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import java.io.File;

/**
 * @author xiongyongcong
 */
public class XIntentUtil {

    public static void startSelectImage(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        activity.startActivityForResult(intent, requestCode);
    }

    public static void startSelectImage(Fragment fragment, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        fragment.startActivityForResult(intent, requestCode);
    }

    public static void startSelectFile(Activity activity, String mimeType, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(mimeType);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void openFile(Activity activity, String filePath) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(filePath)), XMimeUtil.getMimeTypeFromFilePath(filePath));
            activity.startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(activity, "无法打开文件", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}
